/*
   Copyright (c) 2019 Shanghai Xuanzi Technology Co. Ltd https://xuanzi.ltd
   XZMindMap is licensed under the Mulan PSL v1.
   You can use this software according to the terms and conditions of the Mulan PSL v1.
   You may obtain a copy of Mulan PSL v1 at:
      http://license.coscl.org.cn/MulanPSL
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
   PURPOSE.
   See the Mulan PSL v1 for more details.

*/

package xuanzi.openmind.lines;

import xuanzi.commons.graphics.Path;
import xuanzi.commons.graphics.PointF;

public class ArrorUtils {

	public ArrorUtils() { 
	}

	public static class ArrorInfo{
		public PointF start,left,right,mid,end,midfront,midbehind; 
	}
	
	public static ArrorInfo create(PointF start,PointF end,double H ) {
		return create(start, end, H, H*0.7f);
	}
	

	public static ArrorInfo create(PointF start,PointF end  ) {
		   double  H  =   8 ;  // 箭头高度    
	        double  L  =   2 ; // 底边的一半   
	         float  x3  =   0 ;
	         float  y3  =   0 ;
	         float  x4  =   0 ;
	         float  y4  =   0 ;
	        double  awrad  =  Math.atan(L  /  H);  // 箭头角度    
	         double  arraow_len  =  Math.sqrt(L  *  L  +  H  *  H); // 箭头的长度    
	         double [] arrXY_1  =  rotateVec(end.x  -  start.x, end.y  -  start.y, awrad,  true , arraow_len);
	        double [] arrXY_2  =  rotateVec(end.x  -  start.x, end.y  -  start.y,  - awrad,  true , arraow_len);
	        double  x_3  =  end.x  -  arrXY_1[ 0 ];  // (x3,y3)是第一端点    
	         double  y_3  = end.y  -  arrXY_1[ 1 ];
	        double  x_4  =  end.x  -  arrXY_2[ 0 ]; // (x4,y4)是第二端点    
	         double  y_4  =  end.y  -  arrXY_2[ 1 ];
	         ArrorInfo info = new ArrorInfo();
	       Double X3  =   new  Double(x_3);
	       x3  =  X3.floatValue();
	       Double Y3  =   new  Double(y_3);
	       y3  =  Y3.floatValue();
	       Double X4  =   new  Double(x_4);
	       x4  =  X4.floatValue();
	       Double Y4  =   new  Double(y_4);
	       y4  =  Y4.floatValue();
	       info.end = new PointF(end.x, end.y);
	       info.left = new PointF(x3, y3);
	       info.right = new PointF(x4, y4);
	       info.mid = new PointF((x3+x4)/2,(y3+y4)/2);
	       info.start = new PointF(info.mid.x+info.mid.x-end.x,info.mid.y+info.mid.y-end.y);
	       info.midfront = new PointF( (info.mid.x+ info.start.x)/2,(info.mid.y+ info.start.y)/2);
	       info.midbehind = new PointF( (info.mid.x+ info.end.x)/2,(info.mid.y+ info.end.y)/2);
	       return info;
	}
	
	
	public static ArrorInfo create(PointF start,PointF end,double H,double L) {
		//  double  H  =   10 ;  // 箭头高度    
	    //    double  L  =   7 ; // 底边的一半   
	         float  x3  =   0 ;
	         float  y3  =   0 ;
	         float  x4  =   0 ;
	         float  y4  =   0 ;
	        double  awrad  =  Math.atan(L  /  H);  // 箭头角度    
	         double  arraow_len  =  Math.sqrt(L  *  L  +  H  *  H); // 箭头的长度    
	         double [] arrXY_1  =  rotateVec(end.x  -  start.x, end.y  -  start.y, awrad,  true , arraow_len);
	        double [] arrXY_2  =  rotateVec(end.x  -  start.x, end.y  -  start.y,  - awrad,  true , arraow_len);
	        double  x_3  =  end.x  -  arrXY_1[ 0 ];  // (x3,y3)是第一端点    
	         double  y_3  = end.y  -  arrXY_1[ 1 ];
	        double  x_4  =  end.x  -  arrXY_2[ 0 ]; // (x4,y4)是第二端点    
	         double  y_4  =  end.y  -  arrXY_2[ 1 ];
	         ArrorInfo info = new ArrorInfo();
	       Double X3  =   new  Double(x_3);
	       x3  =  X3.floatValue();
	       Double Y3  =   new  Double(y_3);
	       y3  =  Y3.floatValue();
	       Double X4  =   new  Double(x_4);
	       x4  =  X4.floatValue();
	       Double Y4  =   new  Double(y_4);
	       y4  =  Y4.floatValue();
	       info.end = new PointF(end.x, end.y);
	       info.left = new PointF(x3, y3);
	       info.right = new PointF(x4, y4);
	       info.mid = new PointF((x3+x4)/2,(y3+y4)/2);
	       info.start = new PointF(info.mid.x+info.mid.x-end.x,info.mid.y+info.mid.y-end.y);
	       info.midfront = new PointF( (info.mid.x+ info.start.x)/2,(info.mid.y+ info.start.y)/2);
	       info.midbehind = new PointF( (info.mid.x+ info.end.x)/2,(info.mid.y+ info.end.y)/2);
	       return info;
	}
	
	public static void paintArror0(Path path,ArrorInfo info) {
		path.moveTo(info.mid.x, info.mid.y);
		path.lineTo(info.left.x,info.left.y); 
		path.lineTo(info.midbehind.x, info.midbehind.y);
		path.lineTo(info.right.x, info.right.y); 
		path.lineTo(info.mid.x, info.mid.y);
		path.close();
	}
	
	
	public static void paintArror1(Path path,ArrorInfo info) {
		path.moveTo(info.left.x,info.left.y);
		path.lineTo(info.end.x, info.end.y);
		path.lineTo(info.right.x, info.right.y);
		path.lineTo(info.left.x, info.left.y);
		path.close();
	}
	
	public static void paintArror2(Path path,ArrorInfo info) {
		path.moveTo(info.start.x,info.start.y);
		path.lineTo(info.left.x,info.left.y);
		path.lineTo(info.end.x, info.end.y);
		path.lineTo(info.right.x, info.right.y);
		path.lineTo(info.left.x, info.left.y);
		path.close();
	}

	public static void paintArror3(Path path,ArrorInfo info) {
		path.moveTo(info.midbehind.x,info.midbehind.y);
		path.lineTo(info.left.x,info.left.y);
		path.lineTo(info.end.x, info.end.y);
		path.lineTo(info.right.x, info.right.y);
		path.close();
	}
	
	public static void paintArror4(Path path,ArrorInfo info) {
		//path.moveTo(info.start.x,info.start.y);
		path.moveTo(info.left.x,info.left.y);
		path.lineTo(info.end.x, info.end.y);
		path.lineTo(info.right.x, info.right.y);
		path.close();
	}
	
	public static void paintArror5(Path path,ArrorInfo info) {
		path.moveTo(info.left.x,info.left.y);
		path.lineTo(info.end.x, info.end.y);
		path.lineTo(info.right.x, info.right.y);
		path.close();
	}
	
	public static  void  paintArror(Path path,  float  x1,  float  y1,  float  x2,  float  y2)  {

        double  H  =   10 ;  // 箭头高度    
         double  L  =   7 ; // 底边的一半   
         float  x3  =   0 ;
         float  y3  =   0 ;
         float  x4  =   0 ;
         float  y4  =   0 ;
        double  awrad  =  Math.atan(L  /  H);  // 箭头角度    
         double  arraow_len  =  Math.sqrt(L  *  L  +  H  *  H); // 箭头的长度    
         double [] arrXY_1  =  rotateVec(x2  -  x1, y2  -  y1, awrad,  true , arraow_len);
        double [] arrXY_2  =  rotateVec(x2  -  x1, y2  -  y1,  - awrad,  true , arraow_len);
        double  x_3  =  x2  -  arrXY_1[ 0 ];  // (x3,y3)是第一端点    
         double  y_3  =  y2  -  arrXY_1[ 1 ];
        double  x_4  =  x2  -  arrXY_2[ 0 ]; // (x4,y4)是第二端点    
         double  y_4  =  y2  -  arrXY_2[ 1 ];

       Double X3  =   new  Double(x_3);
       x3  =  X3.floatValue();
       Double Y3  =   new  Double(y_3);
       y3  =  Y3.floatValue();
       Double X4  =   new  Double(x_4);
       x4  =  X4.floatValue();
       Double Y4  =   new  Double(y_4);
       y4  =  Y4.floatValue();
       PointF end = new PointF(x2, y2);
       PointF left = new PointF(x3, y3);
       PointF right = new PointF(x4, y4);
       PointF mid = new PointF((x3+x4)/2,(y3+y4)/2);
       PointF start = new PointF(mid.x+mid.x-x2,mid.y+mid.y-y2);
     //  path.moveTo(x1, y1);
       path.moveTo(start.x, start.y);

       path.lineTo(left.x, left.y);
       path.lineTo(end.x, end.y);
       path.lineTo(right.x, right.y);
       path.close();
        // g.setColor(SWT.COLOR_WHITE);
       /* 
       // 画线 
        g.drawLine(x1, y1, x2, y2);
        // 画箭头的一半 
        g.drawLine(x2, y2, x3, y3);
        // 画箭头的另一半 
        g.drawLine(x2, y2, x4, y4);
        g.drawLine(x3, y3,x4, y4);
		*/
       
   } 
	
	 /** 
      *取得箭头的绘画范围
      */  
    public static double [] rotateVec( float  px,  float  py,  double  ang,  boolean  isChLen,
            double  newLen)  { 
        double  mathstr[]  =   new   double [ 2 ];
        // 矢量旋转函数，参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度    
        double  vx  =  px  *  Math.cos(ang)  -  py  *  Math.sin(ang);
        double  vy  =  px  *  Math.sin(ang)  +  py  *  Math.cos(ang);
        if  (isChLen)  {
            double  d  =  Math.sqrt(vx  *  vx  +  vy  *  vy);
           vx  =  vx  /  d  *  newLen;
           vy  =  vy  /  d  *  newLen;
           mathstr[ 0 ]  =  vx;
           mathstr[ 1 ]  =  vy;
       } 
       return  mathstr;
   }
}
